﻿//----------------------------------------------------------------
// <copyright file="UnitTestBase.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NMock2;

namespace Test.UnitTest
{
    /// <summary>
    /// Instantiates and disposes a NUnit Mockery object
    /// </summary>
    /// <remarks>
    /// Tests that use Mockery should inherit this class. NUnit will call
    /// the [TestFixtureTearDown] method when all tests in the derived 
    /// class have completed.
    /// 
    /// Other common test features can be added to this class in the future.
    /// </remarks>
    public class UnitTestBase : IDisposable
    {
        private bool isDisposed;

        public Mockery Mocks { get; private set; }

        public UnitTestBase()
        {
            Mocks = new Mockery();
        }

        ~UnitTestBase()
        {
            Dispose(false);
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }

        [ClassCleanup]
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool isDisposing)
        {
            if (!isDisposed)
            {
                if (isDisposing)
                {
                    if (Mocks != null)
                        Mocks.Dispose();
                }
                Mocks = null;
                isDisposed = true;
            }
        }
    }
}
